//
//  HttpRequest.m
//  iOS实战
//
//  Created by clz on 2019/10/27.
//  Copyright © 2019 clz. All rights reserved.
//

#import "HttpRequest.h"
#import <AFNetworking.h>

static NSString *homeURL = @"/app/manage/initIndex";

typedef NS_ENUM(NSInteger, HttpRequestType) {
    HttpRequestTypeGet          = 0,
    HttpRequestTypePost     = 1,
};

@implementation HttpRequest

+(void)homeRequestWithParameters:(id _Nullable)parameters success:(void(^)(id responseObject))success failure:(void(^)(NSString *errorMessage))failure{
    
    [HttpRequest sendHttpRequestWithRequestType:HttpRequestTypePost host:host requestURL:homeURL parameters:parameters success:success failure:failure];
}

+(AFHTTPSessionManager *)basicConfigure{
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.securityPolicy.allowInvalidCertificates = YES;
    session.operationQueue.maxConcurrentOperationCount = 5;
    session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain", @"text/html",@"image/jpeg", nil];
    [session.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    session.requestSerializer.timeoutInterval = 10;
    return session;
}

+(void)sendHttpRequestWithRequestType:(HttpRequestType)requestType host:(NSString *)host requestURL:(NSString *)requestURL parameters:(id _Nullable)parameters success:(void(^)(id responseObject))success failure:(void(^)(NSString *errorMessage))failure{
    
    AFHTTPSessionManager *session = [HttpRequest basicConfigure];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",host,requestURL];
    
    if (requestType == HttpRequestTypeGet) {
        
        [session GET:url parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            success(responseObject);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            failure(@"请求出错");
        }];
        
    }else if (requestType == HttpRequestTypePost){
        
        
        NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:url parameters:nil error:nil];
        
        NSData *data = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:nil];

        [request setHTTPBody:data];
        [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

        [[session dataTaskWithRequest:request uploadProgress:^(NSProgress * _Nonnull uploadProgress) {
            
        } downloadProgress:^(NSProgress * _Nonnull downloadProgress) {
            
        } completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
            
            if (error) {
                
                failure(@"请求出错");

            }else{
                
                success(responseObject);
                
            }
            
        }]resume];
    }
}



@end
